<?php

class BongkarMuatController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/column2';

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
//			array('allow',  // allow all users to perform 'index' and 'view' actions
//				'actions'=>array('index','view'),
//				'users'=>array('*'),
//			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('create','update','load'),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('load'),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);
                $monitoring=Monitoring::model()->findByPk($model->tb_monitoring_id);
                $kartu = Kartu::model()->findByPk($monitoring->tb_kartu_id);

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['BongkarMuat']))
		{
                    $transaction = Yii::app()->db->beginTransaction();
                    try{
			$model->attributes=$_POST['BongkarMuat'];
                        $model->waktuMulai = new CDbExpression('NOW()');
                        $gudang = Gudang::model()->findByPk($model->ref_gudang_id);
                        $messageType = 'success';
                        $message = Yii::t('controller', '<strong>Well done!</strong> You successfully update data.');
			if($model->save())
                        {
                            $monitoring->statusParkir = 3;
                            $gudang->status = 1;
                            $monitoring->save();
                            $gudang->save();
                            $transaction->commit();
                            Yii::app()->user->setFlash($messageType, $message);
                            $this->redirect(array('viewcheckpoint/'));
                        }
                    }
                    catch (Exception $e)
                    {
                        $transaction->rollBack();
                        Yii::app()->user->setFlash('error', "{$e->getMessage()}");
                    }
		}

		$this->render('update',array(
			'model'=>$model,
                        'kartu'=>$kartu,
		));
	}
        
        //For confirm Un/Load End, update attribute $model->waktuSelesai & $monitoring->statusParkir
        public function actionLoad($id)
        {
                $model = BongkarMuat::model()->with(array(
                    'Gudang' => array('select' => 'Gudang.gate', 'together' => true)
                ))->findByPk($id);
                $gudang = Gudang::model()->findByAttributes(array('gate' => $model->Gudang->gate));
                $c = new CDbCriteria;
                $c->select = 't.tb_kartu_id, t.tb_mobil_id, t.noDokumen';
                $c->with = array(
                    'BongkarMuat' => array(
                        'select' => 'BongkarMuat.tb_monitoring_id, BongkarMuat.statusMobil',
                        'together' => true),
                    'Kartu' => array(
                        'select' => 'Kartu.noTiket',
                        'together' => true),
                    'Mobil' => array(
                        'select' => 'Mobil.noPolisi, Mobil.namaSupir, Mobil.ref_jenis_mobil_id',
                        'together' => true,
                    ),
                );
                $c->condition = 't.id='.$model->tb_monitoring_id;
                $monitoring = Monitoring::model()->find($c);
                $jenisMobil = JenisMobil::model()->findByPk($monitoring->Mobil->ref_jenis_mobil_id);
                
		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);
                
                // Button "Confirm" name = yt1
                if(isset($_POST['yt1']))
		{
                    $transaction = Yii::app()->db->beginTransaction();
                    try{
                        $model->waktuSelesai = new CDbExpression('NOW()');
//                        $redirect = ($model->statusMobil <> 'Bongkar') ? 'ekspedisi/create/'.$model->id : 'viewcheckpoint/';
                        $messageType = 'success';
                        $message = Yii::t('controller', "<strong>Well done!</strong> You successfully update data ");
			if($model->save())
                        {
                            $transaction->commit();
                            $monitoring->statusParkir = 4;
                            $gudang->status = 0;
                            $monitoring->save();
                            $gudang->save();
                            Yii::app()->user->setFlash($messageType, $message);
                            $this->redirect(array('viewcheckpoint/'));
                        }
                    }
                    catch (Exception $e)
                    {
                        $transaction->rollBack();
                        Yii::app()->user->setFlash('error', "{$e->getMessage()}");
                    }
		}

		$this->render('load',array(
			'model'=>$model,
                        'monitoring'=>$monitoring,
                        'jenisMobil'=>$jenisMobil,
		));
        }

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=BongkarMuat::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}
}
